/*
    Copyright (c) <year>, <copyright holder>
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
        * Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
        * Neither the name of the <organization> nor the
        names of its contributors may be used to endorse or promote products
        derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY <copyright holder> ''AS IS'' AND ANY
    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

#include "caltech101.h"

Caltech101::Caltech101 () : Caltech()
{
  setParams ();
  
}

void Caltech101::setParams ()
{
  m_name = "caltech101";


  m_dataset_params.num_cls = 101;
  m_dataset_params.num_img = 8677;// The true value is 9146 when we consider the Google-Background class;
  m_dataset_params.paths.root = PATH_BASE + "/" +  m_name;
  m_dataset_params.paths.images = m_dataset_params.paths.root + "/" + "images";
  
  
  m_img_params.max_image_size = 300;
  
  m_sift_params.grid_spacing = 6;
  m_sift_params.patch_size = 16;
  m_sift_params.bin_size = 4;
  m_sift_params.num_scales = 1;

  m_classif_params.num_tr = 30;
  m_classif_params.num_ts = 30;
  m_classif_params.num_cv = 1;

  //Sizes
  m_cls_names.resize ( m_dataset_params.num_cls );
  m_img_files.resize ( m_dataset_params.num_img );
  m_img_classes.resize ( m_dataset_params.num_img );
}



int Caltech101::setListClasses ( )
{
  DIR *dp;
  struct dirent *dirp;
  unsigned int cpt_cls = 0;

  dp  = opendir ( m_dataset_params.paths.images.c_str() );

  if(dp == NULL)
  {
	  std::cout << "\t Error(" << errno << ") in opening " << m_dataset_params.paths.images << "\n";
	  return errno;
  }
  
  if (VERBOSE)
	std::cout << "Lis of classes : " << "\n";

  while ((dirp = readdir(dp)) != NULL)
  {
	  if (dirp->d_type==DT_DIR && dirp->d_name[0]!= '.')
	  {
		  m_cls_names.at(cpt_cls++) = std::string(dirp->d_name);
		  if (VERBOSE)
			  std::cout << "\t " << cpt_cls << " : " << std::string(dirp->d_name) << "\n";
	  }
  }
  closedir(dp);
  return 0;
}

int Caltech101::setListImages ( const std::string ext )
{
  DIR *dp;
  struct dirent *dirp;
  std::string cext;
  unsigned int cpt_img = 0;

  if (VERBOSE>1)
	std::cout << "Lis of Images : " << "\n";
  
  for (unsigned int cpt_cls=0; cpt_cls<m_cls_names.size(); cpt_cls++)
  {
	std::string subdir ( m_dataset_params.paths.images + "/" + m_cls_names[cpt_cls] );
	dp  = opendir(subdir.c_str());

	if(dp == NULL)
	{
		std::cout << "\t Error(" << errno << ") in opening " << subdir << "\n";
		return errno;
	}
	while ((dirp = readdir(dp)) != NULL)
	{
		if (dirp->d_type!=DT_DIR && dirp->d_name[0]!= '.')
		{
		  unsigned int length = 0;
		  while (dirp->d_name[length] != '\0')
			  length++;

		  for (unsigned int i=0; i<3; i++)
			  cext[i] = dirp->d_name[length-3+i];

		  if (cext.compare(ext))
		  {
			std::string filename ( subdir + "/" + std::string(dirp->d_name) );
			
			m_img_files.at(cpt_img) = filename;
			m_img_classes.at(cpt_img++) = cpt_cls;
			
			if (VERBOSE>1)
				std::cout << "\t " << cpt_img << " : " << filename << "\n";
		  }
		}
	}
	closedir(dp);
  }
  return 0;
}